package com.quanxiaoha.weblog.web.controller;

import com.quanxiaoha.weblog.common.aspect.ApiOperationLog;
import com.quanxiaoha.weblog.common.utils.Response;
import com.quanxiaoha.weblog.web.model.vo.user.EmailLoginReqVO;
import com.quanxiaoha.weblog.web.model.vo.user.SendEmailCodeReqVO;
import com.quanxiaoha.weblog.web.model.vo.user.UpdateUserProfileReqVO;
import com.quanxiaoha.weblog.web.service.EmailService;
import com.quanxiaoha.weblog.web.service.UserProfileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户个人信息控制器
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户个人信息")
@Slf4j
public class UserProfileController {

    @Autowired
    private UserProfileService userProfileService;

    @Autowired
    private EmailService emailService;

    @GetMapping("/profile")
    @ApiOperation(value = "获取当前用户信息")
    @ApiOperationLog(description = "获取当前用户信息")
    public Response getCurrentUserProfile() {
        return userProfileService.getCurrentUserProfile();
    }

    @GetMapping("/profile/{userId}")
    @ApiOperation(value = "获取指定用户公开信息")
    @ApiOperationLog(description = "获取指定用户公开信息")
    public Response getUserPublicProfile(@ApiParam("用户ID") @PathVariable Long userId) {
        return userProfileService.getUserPublicProfile(userId);
    }

    @PostMapping("/profile/update")
    @ApiOperation(value = "更新用户个人信息（旧版本）")
    @ApiOperationLog(description = "更新用户个人信息（旧版本）")
    public Response updateUserProfile(
            @ApiParam("昵称") @RequestParam(required = false) String nickname,
            @ApiParam("头像") @RequestParam(required = false) String avatar,
            @ApiParam("个人简介") @RequestParam(required = false) String bio) {
        return userProfileService.updateUserProfile(nickname, avatar, bio);
    }

    @PostMapping("/profile/update/v2")
    @ApiOperation(value = "更新用户个人信息（新版本）")
    @ApiOperationLog(description = "更新用户个人信息（新版本）")
    public Response updateUserProfileV2(@Validated @RequestBody UpdateUserProfileReqVO updateUserProfileReqVO) {
        return userProfileService.updateUserProfile(updateUserProfileReqVO);
    }

    @PostMapping("/avatar/upload")
    @ApiOperation(value = "上传用户头像")
    @ApiOperationLog(description = "上传用户头像")
    public Response uploadAvatar(@RequestParam("file") MultipartFile file) {
        return userProfileService.uploadAvatar(file);
    }

    @GetMapping("/login-logs")
    @ApiOperation(value = "获取用户登录日志")
    @ApiOperationLog(description = "获取用户登录日志")
    public Response getUserLoginLogs(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("页大小") @RequestParam(defaultValue = "10") Integer size) {
        return userProfileService.getUserLoginLogs(page, size);
    }

    @PostMapping("/email/send-code")
    @ApiOperation(value = "发送邮箱验证码")
    @ApiOperationLog(description = "发送邮箱验证码")
    public Response sendEmailCode(@Validated @RequestBody SendEmailCodeReqVO sendEmailCodeReqVO) {
        return emailService.sendEmailCode(sendEmailCodeReqVO);
    }

    @PostMapping("/email/login")
    @ApiOperation(value = "邮箱验证码登录")
    @ApiOperationLog(description = "邮箱验证码登录")
    public Response emailLogin(@Validated @RequestBody EmailLoginReqVO emailLoginReqVO) {
        return emailService.emailLogin(emailLoginReqVO);
    }

    @PostMapping("/email/bind")
    @ApiOperation(value = "绑定邮箱")
    @ApiOperationLog(description = "绑定邮箱")
    public Response bindEmail(
            @ApiParam("邮箱") @RequestParam String email,
            @ApiParam("验证码") @RequestParam String code) {
        return emailService.bindEmail(email, code);
    }

    @GetMapping("/personal-stats")
    @ApiOperation(value = "获取当前用户个人统计信息")
    @ApiOperationLog(description = "获取当前用户个人统计信息")
    public Response getUserPersonalStats() {
        return userProfileService.getUserPersonalStats();
    }

    @GetMapping("/personal-publish-stats")
    @ApiOperation(value = "获取当前用户个人文章发布统计信息")
    @ApiOperationLog(description = "获取当前用户个人文章发布统计信息")
    public Response getUserPersonalPublishStats() {
        return userProfileService.getUserPersonalPublishStats();
    }

    @GetMapping("/personal-pv-stats")
    @ApiOperation(value = "获取当前用户个人PV访问量统计信息")
    @ApiOperationLog(description = "获取当前用户个人PV访问量统计信息")
    public Response getUserPersonalPVStats() {
        return userProfileService.getUserPersonalPVStats();
    }
}
